/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canteen.sys.model.entity.repository;

import canteen.sys.model.entity.Item;
import canteen.sys.model.generic.GenericHibernateDAOSupport;
import java.util.List;

/**
 *
 * @author TNT
 */
public class ItemDAOImpl extends GenericHibernateDAOSupport<Item, Long> implements ItemDAO {

    @Override
    public List<Item> findAllItemByAuthor(String author, boolean status) {
        List<Item> listItem;
        getSession().beginTransaction();
        listItem = getSession().createQuery("from Item as item "
                + "left join fetch item.author author "
                + "where author.name like :name "
                + "and item.status = :status")
                .setParameter("name", author + "%")
                .setParameter("status", status)
                .list();
        getSession().getTransaction().commit();
        return listItem;
    }

    @Override
    public List<Item> findAllItemByLanguage(String language, boolean status) {
        List<Item> listItem;
        getSession().beginTransaction();
        listItem = getSession().createQuery("from Item as item "
                + "left join fetch item.language language "
                + "where language.name like :name")
                .setParameter("name", language + "%")
                .list();
        getSession().getTransaction().commit();
        return listItem;
    }

    @Override
    public List<Item> findAllItemByOwner(String owner, boolean status) {
        List<Item> listItem;
        getSession().beginTransaction();
        listItem = getSession().createQuery("from Item as item "
                + "left join fetch item.ownerItem owneritem "
                + "where owneritem.nameOfOwner like :name")
                .setParameter("name", owner + "%")
                .list();
        getSession().getTransaction().commit();
        return listItem;
    }

    @Override
    public List<Item> findAllItemByStore(String store,boolean type, boolean status) {
        List<Item> listItem;
        getSession().beginTransaction();
        listItem = getSession().createQuery("from Item as item "
                + "left join fetch item.store store "
                + "left join fetch item.author author "
                + "where store.name like :name "
                + "and author.typeAuthor = :type")
                .setParameter("name", store + "%")
                .setParameter("type", type)
                .list();
        getSession().getTransaction().commit();
        return listItem;
    }

    @Override
    public List<Item> findAllItemByTypeOfUse(String typeOfUse, boolean status) {
        List<Item> listItem;
        getSession().beginTransaction();
        listItem = getSession().createQuery("from Item as item "
                + "left join fetch item.typeOfUse typeofuse "
                + "where typeofuse.name like :name")
                .setParameter("name", typeOfUse + "%")
                .list();
        getSession().getTransaction().commit();
        return listItem;
    }

    @Override
    public List<Item> findAllItemByStatusOfUse(String statusOfUse, boolean status) {
        List<Item> listItem;
        getSession().beginTransaction();
        listItem = getSession().createQuery("from Item as item "
                + "left join fetch item.statusOfUse statusofuse "
                + "where statusofuse.name like :name")
                .setParameter("name", statusOfUse + "%")
                .list();
        getSession().getTransaction().commit();
        return listItem;
    }

    @Override
    public List<Item> findAllItemByTypeOfRegulation(String typeOfRegulation, boolean status) {
        List<Item> listItem;
        getSession().beginTransaction();
        listItem = getSession().createQuery("from Item as item "
                + "left join fetch item.typeOfRegulation typeofregulation "
                + "where typeofregulation.name like :name")
                .setParameter("name", typeOfRegulation + "%")
                .list();
        getSession().getTransaction().commit();
        return listItem;
    }
    
}
